<?php include('top.php'); ?>
<!-- 轮播图 -->
<style>
    .hero-section {
            background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('school.jpg');
            background-size: cover;
            height: 60vh;
            color: white;
    }
    
    .date {
        padding: 6px;
    }

    .today-circle {
        display: inline-block;
        width: 2rem;
        height: 2rem;
        border-radius: 50%;
        background-color: white;
        color: #0d6efd;
        line-height: 2rem;
    }

    .column-card {
        margin-bottom: 1.5rem;
    }

    .article-image {
        width: 100%;
        height: 120px;
        object-fit: cover;
        border-radius: 0.25rem;
    }

    .article-link {
        text-decoration: none;
        color: inherit;
    }

    .article-link:hover {
        color: #0d6efd;
    }

    .more-link {
        display: inline-block;
        margin-top: 1rem;
    }

    .article-item {
        padding-bottom: 1rem;
        margin-bottom: 1rem;
        border-bottom: 1px solid #dee2e6;
        /* 文章之间添加分隔线 */
    }

    .publish-time {
        color: #6c757d;
        font-size: 0.875rem;
    }

    .article-summary {
        margin-top: 0.5rem;
        color: #495057;
    }
</style>

<div id="schoolCarousel" class="carousel slide" data-bs-ride="carousel" data-bs-interval="3000">
    <div class="carousel-indicators">
        <?php
        $banner_sql = "SELECT * FROM web_banners";
        $banner_stmt = $link->prepare($banner_sql);
        $banner_stmt->execute();
        $banner_result = $banner_stmt->get_result();
        $banner_count = $banner_result->num_rows;
        for ($i = 0; $i < $banner_count; $i++) { ?>
            <button type="button" data-bs-target="#schoolCarousel"
                data-bs-slide-to="<?= $i ?>" <?= $i == 0 ? 'class="active"' : '' ?>></button>
        <?php } ?>
    </div>
    <div class="carousel-inner">
        <?php
        $active = true;
        $banner_result->data_seek(0); // 重置指针
        while ($banner_row = $banner_result->fetch_assoc()) { ?>
            <div class="carousel-item <?= $active ? 'active' : '' ?>">
                <div class="hero-section d-flex align-items-center justify-content-center"
                    style="background-image: url('<?= htmlspecialchars($banner_row['url']) ?>')">
                    <div class="text-center text-white">
                        <h1 class="display-3 fw-bold"><?= htmlspecialchars($banner_row['title']) ?></h1>
                        <p class="lead"><?= htmlspecialchars($banner_row['slogan']) ?></p>
                    </div>
                </div>
            </div>
        <?php $active = false;
        } ?>
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#schoolCarousel" data-bs-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#schoolCarousel" data-bs-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
    </button>
</div>

<div class="p-3 mb-2 bg-primary text-white text-center" style="font-size: 1.5em;">
    <?php
    $today = date('j');
    echo "<span style=\"font-size: 2em\">" . date("n") . "</span>月 ";
    foreach (new DatePeriod(new DateTime('first day of this month'), new DateInterval('P1D'), new DateTime('first day of next month')) as $d) {
        if ($d->format('j') == $today) {
            echo '<span class="today-circle">' . $d->format('d') . '</span>';
        } else {
            echo '<span class="date">' . $d->format('j') . '</span>';
        }
    }
    ?>
</div>

<?php
// 获取伪静态配置
$is_static = isset($webinfo_row['static']) ? $webinfo_row['static'] : 0;

// 生成栏目链接函数
function generate_menu_link($wbtreeid, $is_static)
{
    return $is_static
        ? "/list/list-{$wbtreeid}.htm"
        : "list.php?wbtreeid={$wbtreeid}";
}

// 生成文章链接函数（增加外链判断）
function generate_article_link($article, $is_static)
{
    // 存在外链则直接返回外链地址
    if (!empty($article['url'])) {
        return $article['url'];
    }
    // 否则生成正常链接
    return $is_static
        ? "/article/article-{$article['wbtreeid']}-{$article['id']}.htm"
        : "article.php?wbtreeid={$article['wbtreeid']}&id={$article['id']}";
}

$menu_query = "SELECT * FROM article_menu WHERE status != 0";
$menu_stmt = mysqli_prepare($link, $menu_query);
mysqli_stmt_execute($menu_stmt);
$menu_result = mysqli_stmt_get_result($menu_stmt);

$columns = [];
while ($menu = mysqli_fetch_assoc($menu_result)) {
    // 查询时需要包含url字段
    $article_query = "SELECT * FROM article_article 
                     WHERE wbtreeid = ? ORDER BY id DESC LIMIT 10";
    $article_stmt = mysqli_prepare($link, $article_query);
    mysqli_stmt_bind_param($article_stmt, "i", $menu['wbtreeid']);
    mysqli_stmt_execute($article_stmt);
    $article_result = mysqli_stmt_get_result($article_stmt);

    $articles = [];
    while ($article = mysqli_fetch_assoc($article_result)) {
        $articles[] = $article;
    }

    $columns[] = [
        'menu' => $menu,
        'articles' => $articles
    ];
    mysqli_stmt_close($article_stmt);
}
?>
<div class="container py-4">
    <div class="row g-4">
        <?php foreach ($columns as $index => $col):
            $count = count($columns);
            if ($count === 1) {
                $colClass = 'col-12';
            } elseif ($count === 2) {
                $colClass = 'col-md-6';
            } else {
                $colClass = 'col-md-4';
            }
        ?>
            <div class="<?= $colClass ?>">
                <div class="card column-card">
                    <div class="card-header">
                        <h4 class="mb-0">
                            <?= htmlspecialchars($col['menu']['menu']) ?>
                        </h4>
                    </div>
                    <div class="card-body">
                        <?php foreach ($col['articles'] as $article):
                            // Check if there is an image in the article content
                            $hasImage = preg_match('/<img.*?src=[\'"](.*?)[\'"].*?>/', $article['content'], $matches);
                            // 格式化时间戳为日期
                            $publishTime = date('Y-m-d H:i:s', $article['time']);
                        ?>
                            <div class="article-item">
                                <div class="row align-items-center">
                                    <?php if ($hasImage): ?>
                                        <div class="col-md-4">
                                            <img src="<?= htmlspecialchars($matches[1]) ?>" class="article-image" alt="Article Image">
                                        </div>
                                        <div class="col-md-8">
                                            <div class="d-flex justify-content-between align-items-start">
                                                <a href="<?= generate_article_link($article, $is_static) ?>"
                                                    <?= !empty($article['url']) ? 'target="_blank"' : '' ?>
                                                    class="article-link">
                                                    <?= htmlspecialchars($article['title']) ?>
                                                </a>
                                                <span class="publish-time"><?= $publishTime ?></span>
                                            </div>
                                            <p class="article-summary"><?= htmlspecialchars($contentSummary) ?></p>
                                        </div>
                                    <?php else: ?>
                                        <div class="col-12">
                                            <div class="d-flex justify-content-between align-items-start">
                                                <a href="<?= generate_article_link($article, $is_static) ?>"
                                                    <?= !empty($article['url']) ? 'target="_blank"' : '' ?>
                                                    class="article-link">
                                                    <?= htmlspecialchars($article['title']) ?>
                                                </a>
                                                <span class="publish-time"><?= $publishTime ?></span>
                                            </div>
                                            <p class="article-summary"><?= htmlspecialchars($contentSummary) ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="card-footer bg-transparent">
                        <a class="more-link" href="<?= generate_menu_link($col['menu']['wbtreeid'], $is_static) ?>">
                            更多
                        </a>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>



<?php include('bottom.php'); ?>
</body>

</html>